package edu.xmu.software.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import edu.xmu.software.dao.ItemDAO;
import edu.xmu.software.pojo.Item;
import edu.xmu.software.util.ParamUtils;

public class ShowListsController extends MultiActionController {

	private ItemDAO itemDAO;
	private int itemsPerPage = 3;
	
	public void setItemDAO(ItemDAO itemDAO) {
		this.itemDAO = itemDAO;
	}

	public ModelAndView word(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		List<Item> itemsList = null;
		Map<String, Object> retValue = new HashMap<String, Object>();
		
		int page = ParamUtils.getIntParameter(request, "page", 1);
		int offset = (page-1) * itemsPerPage;
		
		itemsList = itemDAO.getWordItems(offset, itemsPerPage);
		
		int next = 0;
		int previous = page - 1;
		if (itemDAO.getWordItems(offset+itemsPerPage, itemsPerPage).size() != 0)
			next = page+1;
		
		retValue.put("next", next);
		retValue.put("previous", previous);
		
		retValue.put("itemslist", itemsList);
		return new ModelAndView("itemslist", retValue);
	}
	
	public ModelAndView file(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		List<Item> itemsList = null;
		Map<String, Object> retValue = new HashMap<String, Object>();
		
		int page = ParamUtils.getIntParameter(request, "page", 1);
		int offset = (page-1) * itemsPerPage;
		
		itemsList = itemDAO.getFileItems(offset, itemsPerPage);
		
		int next = 0;
		int previous = page - 1;
		if (itemDAO.getFileItems(offset+itemsPerPage, itemsPerPage).size() != 0)
			next = page+1;
		
		retValue.put("next", next);
		retValue.put("previous", previous);
		
		retValue.put("itemslist", itemsList);
		return new ModelAndView("itemslist", retValue);
	}
	
	public ModelAndView catelog(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		List<Item> itemsList = null;
		Map<String, Object> retValue = new HashMap<String, Object>();
		
		int page = ParamUtils.getIntParameter(request, "page", 1);
		int offset = (page-1) * itemsPerPage;
		
		itemsList = itemDAO.getCatelogItems(offset, itemsPerPage);
		
		int next = 0;
		int previous = page - 1;
		if (itemDAO.getCatelogItems(offset+itemsPerPage, itemsPerPage).size() != 0)
			next = page+1;
		
		retValue.put("next", next);
		retValue.put("previous", previous);
		
		retValue.put("itemslist", itemsList);
		return new ModelAndView("itemslist", retValue);
	}
	
	public ModelAndView newest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		List<Item> itemsList = null;
		Map<String, Object> retValue = new HashMap<String, Object>();
		
		itemsList = itemDAO.getLatestItems(20);
		
		retValue.put("itemslist", itemsList);
		return new ModelAndView("itemslist", retValue);
	}
}
